package com.wbl.train.business.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wbl.train.common.business.dto.TrainCarriageDto;
import com.wbl.train.common.business.entity.TrainCarriageEntity;
import com.wbl.train.business.mapper.TrainCarriageMapper;
import com.wbl.train.business.service.TrainCarriageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wbl.train.common.business.enums.SeatColEnum;
import com.wbl.train.common.exception.ApplicationException;
import com.wbl.train.common.page.PageVo;
import com.wbl.train.common.result.ResultCode;
import com.wbl.train.common.utils.SnowUtil;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 火车车厢 服务实现类
 * @author 王博纶
 * @since 2024-02-02
 */
@Service
public class TrainCarriageServiceImpl extends ServiceImpl<TrainCarriageMapper, TrainCarriageEntity> implements TrainCarriageService {

    @Override
    public PageVo<TrainCarriageEntity> queryTrainCarriage(TrainCarriageDto trainCarriageDto) {
        // 获取分页所需要的参数
        int page = trainCarriageDto.getPage();
        int limit = trainCarriageDto.getLimit();

        LambdaQueryWrapper<TrainCarriageEntity> trainCarriageWrapper = new LambdaQueryWrapper<TrainCarriageEntity>()
                .orderByAsc(TrainCarriageEntity::getTrainCode) // 按照trainCode升序排列
                .orderByAsc(TrainCarriageEntity::getCarriageIndex) // 按照carriageIndex升序排列
                // 根据trainCode进行过滤，有值就过滤，没有值就不拼接条件
                .eq(ObjectUtil.isNotEmpty(trainCarriageDto.getTrainCode()),TrainCarriageEntity::getTrainCode,trainCarriageDto.getTrainCode());
        //创建分页对象
        IPage<TrainCarriageEntity> trainCarriagePage = new Page<>(page,limit);
        //调用分页方法
        IPage<TrainCarriageEntity> trainCarriagePageData = page(trainCarriagePage,trainCarriageWrapper);
        // 获取查询出来的数据集合
        List<TrainCarriageEntity> records = trainCarriagePageData.getRecords();
        // 将数据塞到PageVo类中并返回出去
        return new PageVo<TrainCarriageEntity>()
                .setDataList(records)
                .setPageSize(limit).setCurrentPage(page)
                .setTotal((int) trainCarriagePageData.getTotal())
                .setPages((int) trainCarriagePageData.getPages());
    }

    @Override
    public boolean insertAndUpdateTrain(TrainCarriageEntity trainCarriage) {
        // 获取当前的时间 --- 用于创建和修改的时候的时间填充
        DateTime now = DateTime.now();
        // 设置火车车站的修改时间
        trainCarriage.setUpdateTime(now);

        // 自动计算列数和总座位数
        List<SeatColEnum> seatCols = SeatColEnum.getColsByType(trainCarriage.getSeatType());
        trainCarriage
                // 设置列数 --- 列数为枚举类的长度
                .setColCount(seatCols.size())
                // 设置总座位数 --- 总座位数 = 列数 * 行数
                .setSeatCount(seatCols.size() * trainCarriage.getRowCount());
        // 判断Id是否为空
        if(ObjectUtil.isNotNull(trainCarriage.getId())){
            // 根据id查询火车车站信息
            TrainCarriageEntity trainCarriageEntity = this.getById(trainCarriage.getId());
            // 当前id对应的无火车车站信息
            if(ObjectUtil.isNull(trainCarriageEntity)){
                throw new ApplicationException(ResultCode.DATA_NOT_EXIST,"当前火车车厢不存在");
            }
            // 更新火车车次信息
            return this.updateById(trainCarriage);
        }

        // 通过trainCode和carriageIndex进行火车车站信息的查询
        TrainCarriageEntity trainCarriageEntity = getTrainCarriageByUniqueKey(trainCarriage.getTrainCode(),trainCarriage.getCarriageIndex());
        // 查询出的trainEntity不为空，不允许新增
        if(ObjectUtil.isNotEmpty(trainCarriageEntity)){
            throw new ApplicationException(ResultCode.DATA_EXIST,"当前火车车厢已存在");
        }
        // 给新增的火车车站设置Id，创建时间和修改时间
        trainCarriage.setId(SnowUtil.getSnowflakeNextId())
                .setCreateTime(now);
        // 新增火车车站
        return this.save(trainCarriage);
    }

    /**
     * 根据唯一键查询 trainCarriage 火车车厢信息
     * @param trainCode 火车车次编号
     * @param carriageIndex 车厢序号
     * @return trainCarriage 火车车厢信息
     */
    private TrainCarriageEntity getTrainCarriageByUniqueKey(String trainCode,Integer carriageIndex) {
        LambdaQueryWrapper<TrainCarriageEntity> wrapper = new LambdaQueryWrapper<TrainCarriageEntity>()
                .eq(TrainCarriageEntity::getTrainCode, trainCode)
                .eq(TrainCarriageEntity::getCarriageIndex, carriageIndex);
        return this.getOne(wrapper);
    }

    public List<TrainCarriageEntity> selectByTrainCode(String trainCode){
        LambdaQueryWrapper<TrainCarriageEntity> wrapper = new LambdaQueryWrapper<TrainCarriageEntity>()
                .eq(TrainCarriageEntity::getTrainCode, trainCode)
                .orderByAsc(TrainCarriageEntity::getCarriageIndex);
        return this.list(wrapper);
    }
}
